
import 'package:bt_tool/basic/bt_api_manager.dart';
import 'package:bt_tool/basic/bt_app_info.dart';
import 'package:bt_tool/basic/bt_db_tool.dart';
import 'package:bt_tool/basic/bt_log_tool.dart';
import 'package:bt_tool/basic/bt_notification_center.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/basic/bt_router_define.dart';
import 'package:bt_tool/basic/bt_theme.dart';
import 'package:bt_tool/basic/bt_theme_color.dart';
import 'package:bt_tool/basic/bt_user_tool.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/common/config/app_bridge.dart';
import 'package:flutter_demo/common/config/app_translate_config.dart';
import 'package:flutter_demo/tools/project/project_tool.dart';
import 'package:flutter_demo/tools/tools_main_page.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'common/config/app_config.dart';
import 'common/config/app_router.dart';

void main() {
  // debugPaintSizeEnabled = true;

  WidgetsFlutterBinding.ensureInitialized();
  loadAsyncInfo();
}

void loadAsyncInfo() async {
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
  };

  await BTLogTool.share().initConfig();
  btLogSavePrint("app open");
  await BTAppInfo.loadInfo();
  await BTDbTool.share().initializeDatabase(2, (db, oldVersion, newVersion){
    btLog2("BTDbTool","oldVersion:$oldVersion;newVersion:$newVersion");
  });
  BTRouter.share().routerProtocol = AppRouterLogic();
  await AppConfig.configApi();
  btLogSave("当前API环境:${BTApiManager.share().selModel.convertToJsonStr()}",isPrint: true);
  btLogSavePrint("基本信息:${await BTAppInfo.getAllBasicInfo()}");
  if (kReleaseMode) {
    btLogSavePrint('应用当前运行在 Release (AOT) 模式');
  } else if (kProfileMode) {
    btLogSavePrint('应用当前运行在 Profile (AOT 但带调试工具) 模式');
  } else if (kDebugMode) {
    btLogSavePrint('应用当前运行在 Debug (JIT) 模式');
  }
  BTTheme.share().registerTheme(["default"], "default");
  AppConfig.configTheme();
  AppTranslate.config();
  await ProjectTool.createProjectTable();
  AppBridgeTool.share().register();
  runApp(const MyApp());

}




class MyApp extends StatelessWidget with BTNotificationProtocol {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: BTAppInfo.getAppName(),
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: BTThemeColor.mainColor),
        useMaterial3: true,
      ),
      initialRoute: BTRouterDefine.btMainTabbarPageRouterName,
      routes: {
        BTRouterDefine.btMainTabbarPageRouterName:(context) => const ToolsMainPage(),
      },
      builder: EasyLoading.init(),
      navigatorObservers: [
        BTRouteObserver.routeObserver,
      ],
    );

  }
}


